package com.sl.mes.app.service.mm;

import com.sl.mes.app.dao.mm.AggMoReqpickmVo;
import com.sl.mes.db.itf.mm.IMmReqpickm;
import com.sl.mes.db.vo.MmPickmB;
import com.sl.mes.db.vo.MmReqpickm;
import com.sl.mes.db.vo.MmReqpickmB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MmReqpickmService {

    @Autowired
    private IMmReqpickm mmReqpickmService;

    /**
     * 根据id查询配料单
     * @param creqpickmid
     * @return
     */
    public AggMoReqpickmVo getAggMoReqpickmVoById(String creqpickmid){
        MmReqpickm  mmReqpickm = mmReqpickmService.findParentVoById(creqpickmid);
        List<MmReqpickmB> lst = mmReqpickmService.findChildrenVoById(creqpickmid);
        AggMoReqpickmVo aggMoReqpickmVo = new AggMoReqpickmVo();
        aggMoReqpickmVo.setMmReqpickmHeadVo(mmReqpickm);
        aggMoReqpickmVo.setMmReqpickmBBodyVo(lst);
        return aggMoReqpickmVo;
    }

    /**
     * 根据id删除配料单
     * @param creqpickmid
     * @return
     */
    public AggMoReqpickmVo deleteAggMoReqpickmVoById(String creqpickmid){
        //把要删除的数据返回给客户端
        MmReqpickm mmReqpickm =mmReqpickmService.findParentVoById(creqpickmid);
        List<MmReqpickmB> lst =  mmReqpickmService.findChildrenVoById(creqpickmid);
        AggMoReqpickmVo aggMoReqpickmVo = new AggMoReqpickmVo();
        aggMoReqpickmVo.setMmReqpickmHeadVo(mmReqpickm);
        aggMoReqpickmVo.setMmReqpickmBBodyVo(lst);
        //根据id进行删除
         mmReqpickmService.deleteParentVoById(creqpickmid);
         mmReqpickmService.deleteChildrenVoById(creqpickmid);

        return aggMoReqpickmVo;
    }

    /**
     * 添加配料
     * @param aggMoReqpickmVo
     * @return
     */
    public int addAggMoReqpickmVo(AggMoReqpickmVo aggMoReqpickmVo){
        MmReqpickm  mmReqpickm = aggMoReqpickmVo.getMmReqpickmHeadVo();
        List<MmReqpickmB> lstMmReqpickmB  =aggMoReqpickmVo.getMmReqpickmBBodyVo();
        int res = mmReqpickmService.insertMmReqpickm(mmReqpickm,lstMmReqpickmB);
        return res;
    }

    /**
     * 修改配料单
     * @param aggMoReqpickmVo
     * @return
     */
    public int updateAggMoReqpickmVo(AggMoReqpickmVo aggMoReqpickmVo){
        MmReqpickm  mmReqpickm = aggMoReqpickmVo.getMmReqpickmHeadVo();
        List<MmReqpickmB> lstMmReqpickmB  =aggMoReqpickmVo.getMmReqpickmBBodyVo();
        mmReqpickmService.updateMmReqpickm(mmReqpickm);
        int res =mmReqpickmService.updateMmReqpickmB(lstMmReqpickmB);
        return res;
    }


    /**
     * 根据成产订单查询对应的物料信息
     * @param cmoid
     * @return
     */
    public List<MmReqpickmB> selectMmReqpickmBByCmoid(String cmoid){
       return mmReqpickmService.selectMmReqpickmBByComid(cmoid);
    }
}
